<template>
<!-- 保单录入-再保操作页面 -->
    <div class="fillcontain">
        <div class="contain">
            <!-- 操作按钮 -->
            <el-row> 
                <el-col :span="24" class="pt5">
                    <el-button type="danger" plain @click="addInfo">增加</el-button>
                </el-col>
            </el-row>

            <!-- 数据 -->
            <div class="table_container mt20">
                <el-table
                 v-loading="loading"
                 :data="tableData"
                  @row-click="rowClick"
                 highlight-current-row
                 header-cell-class-name="table-header-class"
                 style="width:100%">
                <el-table-column
                width="120"
                    property="sqno"
                    label="序号"
                    align='center'
                    show-overflow-tooltip>
                    <template slot-scope="scope">
                        <el-input :readonly="true" v-model="scope.row.sqno" placeholder=""></el-input>
                    </template>
                </el-table-column>
                <el-table-column
                    width="120"
                   property="chdrnum"
                   label="保单号"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <el-input :readonly="true" v-model="scope.row.chdrnum" placeholder=""></el-input>
                    </template>
                </el-table-column>
                <el-table-column
                 width="120"
                   property="surname"
                   label="被保人"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <el-input :readonly="true" v-model="scope.row.surname" placeholder=""></el-input>
                    </template>
                </el-table-column>
                 <el-table-column
                 width="120"
                   property="lifcnum"
                   label="客户号"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <el-input :readonly="true" v-model="scope.row.lifcnum" placeholder=""></el-input>
                    </template>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="cltsex"
                   label="性别"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <!-- <el-input :readonly="true"   v-model="scope.row.cltsex" placeholder=""></el-input> -->
                        <el-select v-model="scope.row.cltsex" :disabled="true" placeholder="" >
                            <el-option
                                v-for="item in sexs"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column> 
                  <el-table-column
                    width="120"
                   property="anbAtCcd"
                   label="年龄"
                   align='center'
                   show-overflow-tooltip>
                    <template slot-scope="scope">
                        <el-input :readonly="true" v-model="scope.row.anbAtCcd" placeholder=""></el-input>
                    </template>
                </el-table-column> 
                <el-table-column
                width="120"
                   property="statcode"
                   label="保单状态"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <el-input :readonly="true" v-model="scope.row.statcode" placeholder=""></el-input>
                    </template>
                   </el-table-column>
                <el-table-column
                 width="120"
                   property="mortcls"
                   label="职级"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <el-input :readonly="true" v-model="scope.row.mortcls" placeholder=""></el-input>
                    </template>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="crtable"
                   label="分保类型"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <el-select v-model="scope.row.crtable" @change="checkCrtable(scope.row)" :disabled="disabledInput(scope.row.rownum)" placeholder="" >
                            <el-option
                                v-for="item in crtableDetailList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="sumins"
                   label="保额"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <!-- <el-input :readonly="disabledInput(scope.row.rownum)" v-model="scope.row.sumins" placeholder=""></el-input> -->
                        <el-input-number v-model="scope.row.sumins" :controls="false" :disabled="disabledInput(scope.row.rownum)"></el-input-number>
                    </template>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="rscompcode"
                   label="分保公司"
                   align='center'
                   show-overflow-tooltip>
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.rscompcode" :disabled="disabledInput(scope.row.rownum)" placeholder="" >
                            <el-option
                                v-for="item in rscompcodeList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="riraree"
                   label="分保比例"
                   align='center'
                   show-overflow-tooltip>
                    <template slot-scope="scope">
                        <!-- <el-input :readonly="disabledInput(scope.row.rownum)" v-model="scope.row.riraree" placeholder=""></el-input> -->
                        <el-input-number v-model="scope.row.riraree" @change="checkRiraree(scope.row)" :max="1" :controls="false" :disabled="disabledInput(scope.row.rownum)"></el-input-number>
                    </template>
                </el-table-column> 
                <el-table-column
                 width="180"
                   property="bmntdesc"
                   label="再保意见"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <el-input :readonly="disabledInput(scope.row.rownum)" maxlength="100" v-model="scope.row.bmntdesc" placeholder=""></el-input>
                    </template>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="effdate"
                   label="分保生效日"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <el-input :readonly="true" v-model="scope.row.effdate" placeholder=""></el-input>
                    </template>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="validflag"
                   label="有效标志"
                   align='center'
                   show-overflow-tooltip>
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.validflag" :disabled="scope.row.initValidflag == '2'" @change="validflagChange(scope.row)" placeholder="" >
                            <el-option
                                v-for="item in validflags"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                        <!-- <el-input :readonly="true" v-model="scope.row.validflag" placeholder=""></el-input> -->
                    </template>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="cncldesc"
                   label="作废意见"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <el-input :readonly="!scope.row.isEdit" maxlength="100" v-model="scope.row.cncldesc" placeholder=""></el-input>
                    </template>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="crtuser"
                   label="记录人员"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <el-input :readonly="true" v-model="scope.row.crtuser" placeholder=""></el-input>
                    </template>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="strdate"
                   label="记录日期"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <el-input :readonly="true" v-model="scope.row.strdate" placeholder=""></el-input>
                    </template>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="chaguser"
                   label="作废人员"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <el-input :readonly="true" v-model="scope.row.chaguser" placeholder=""></el-input>
                    </template>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="enddate"
                   label="作废日期"
                   align='center'
                   show-overflow-tooltip>
                   <template slot-scope="scope">
                        <el-input :readonly="true" v-model="scope.row.enddate" placeholder=""></el-input>
                    </template>
                </el-table-column> 
                 <el-table-column 
                    fixed="right"
                   label="操作"
                   align='center'>
                    <template slot-scope="scope">
                        <el-button type="text" size="small" @click="deleteInfo(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
                </el-table>
            </div>

            <!-- 再保意见 -->
            <div class="title mt20">
                <span>再保意见</span>
            </div>
            <el-row>
                <el-col :span="24">
                    <el-input type="textarea" :value="bmntdescTemp" :readonly="true"></el-input>
                </el-col>
            </el-row>

            <!-- 作废意见 -->
            <div class="title mt20">
                <span>作废意见</span>
            </div>
            <el-row>
                <el-col :span="24">
                    <el-input type="textarea" :value="cncldescTemp" :readonly="true"></el-input>
                </el-col>
            </el-row>

            <!-- 保存按钮 -->
            <el-row class="mt20 pb20">
                <el-col :span="24" class="text_center">
                    <el-button type="danger" class="Defaultbutton" @click='saveInfo'>保存</el-button>
                    <el-button type="danger" @click='goBack'>返回</el-button>
                </el-col>
            </el-row>
        </div>
    </div>
</template>

<script>
    import { mapState, mapGetters } from 'vuex'
    import constant from '@/utils/constant';
    import * as api from "@/api/policyRecord/reinsurance/reinsuranceInfo.js";
    export default {
        data(){
            return {
                tableData: [],
                //新增信息
                addInfos: [],
                //更新信息
                updateInfos: [],
                //删除信息
                deleteInfos: [],
                validflags: [
                    {
                        value: '1',
                        label: '有效'
                    },
                    {
                        value: '2',
                        label: '作废'
                    }
                ],
                sexs: [
                    {
                        value: 'F',
                        label: '女性'
                    },
                    {
                        value: 'M',
                        label: '男性'
                    },
                    {
                        value: 'U',
                        label: '不明'
                    },
                ],
                inseagnVo: {
                    is_applnum: '',
                    ls_coy: '',
                    s_statcode: '',
                    s_cltnum: '',
                    s_mortcls: '',
                    s_anb_at_ccd: '',
                    s_surname: '',
                    s_cltsex: '',
                    statcode1: '',
                },
                listCrtable: [],
                //下拉险种代码
                crtableDetailList: [],
                //分保公司
                rscompcodeList: [],
                listCoy: [],
                addAccessionFlag: '',
                //再保意见
                bmntdescTemp: '',
                //作废意见
                cncldescTemp: '',
                gnvSysIsUserId: '',
                nTime: '',
                modifyChdrnum: '',
                selectRow: {},
                inseagnVo: {
                    is_applnum: '',
                    ls_coy: '',
                    s_statcode: '',
                    s_cltnum: '',
                    s_mortcls: '',
                    s_anb_at_ccd: '',
                    s_surname: '',
                    s_cltsex: '',
                    statcode1: '',
                },
                tempInfo: {
                    sqno: '',
                    chdrnum: '',
                    surname: '',
                    lifcnum: '',
                    cltsex: '',
                    anbAtCcd: '',
                    statcode: '',
                    mortcls: '',
                    crtable: '',
                    sumins: '',
                    rscompcode: '',
                    riraree: '',
                    bmntdesc: '',
                    effdate: '',
                    validflag: '',
                    cncldesc: '',
                    crtuser: '',
                    strdate: '',
                    chaguser: '',
                    enddate: '',
                    modifyFlag: 'N',
                    saflag: '9',
                    sredno: '',
                    rownum: '',
                },
                loading:false,
            }
        },
        mounted() {
            this.findCrtableListByChdrnum();
            this.findRscompcodeList();
        },
        methods: {
          clearTag(){
            let that = this;
            let activeTag = JSON.parse(sessionStorage.getItem('tagActive'));
            if(activeTag.length > 1){
              activeTag.map((item,index) =>{
                // debugger
                if(item.path == that.$route.path)
                  activeTag.splice(index,1);
              })
            } else if (activeTag.length == 1) {
              activeTag.map((item,index) =>{
                // debugger
                if(item.path == that.$route.path && item.path !='/home/taskHome')
                  activeTag.splice(index,1);
              })
            }
            sessionStorage.setItem("tagActive",JSON.stringify(activeTag));
            that.$store.dispatch('setTagActive',activeTag);
          },
             // 返回
            goBack(){
                this.clearTag();
                this.$router.push({path:'/home/policyRecord/reinsuranceInfo'})
            },
            initData() {
                this.modifyChdrnum = this.$route.params.chdrnum;
                let chdrnum = this.$route.params.chdrnum;
                let applnum = this.$route.query.applnum;
                let para = {
                    chdrnum: chdrnum,
                    queryFlag: 'Y'
                };
                if(applnum) {
                    console.log(this.$route.query.applnum)
                    para = this.$route.query;
                    api.addAccession(para).then(res =>{
                        if(res.resultCode == constant.SUCCESS){
                            if(res.data != null && res.data != undefined){
                                this.tableData = res.data.urls;
                                this.tableData.forEach(item=>{
                                  item['isEdit'] = false;
                                })
                                this.addAccessionFlag = res.data.addAccession;
                                this.listCoy = res.data.listCoy;
                                this.rscompcodeList = res.data.listCoy;
                                this.listCrtable = res.data.listCrtable;
                                this.crtableDetailList = res.data.listCrtable;
                                this.nTime = res.data.nTime;
                                this.gnvSysIsUserId = res.data.gnvSysIsUserId;
                            }
                        }
                    }).catch(err =>{
                        console.log(err);
                    })
                } else {
                    api.getReinsureList(para).then(res =>{
                        if(res.resultCode == constant.SUCCESS){
                            if(res.data != null && res.data != undefined){
                                this.tableData = res.data.urls;
                              this.tableData.forEach(item=>{
                                item['isEdit'] = false;
                              })
                                this.nTime = res.data.nTime;
                                this.gnvSysIsUserId = res.data.gnvSysIsUserId;
                            }
                        }
                    }).catch(err =>{
                        console.log(err);
                    })
                }
            },
            rowClick(row) {
                this.selectRow = row;
                this.bmntdescTemp = row.bmntdesc; //再保意见
                this.cncldescTemp = row.cncldesc; //作废意见
            },
            sexFormatter(value) {
                console.log("=============="+value)
                // == 'F' ? '女性' : scope.row.cltsex == 'M' ? '男性' : '不明'
                let result = "";
                if (value == 'F') {
                    result = "女性"
                } else if (value == 'M') {
                    result = "男性"
                } else if (value == 'U') {
                    result = "不明"
                }
                return result;
            },
            disabledInput(rownum) {
                console.log("rownum======="+rownum)
                if(rownum == null || rownum == undefined || rownum == "") {
                    return false;
                } else {
                    return true;
                }
            },
            resetTempInfo() {
                this.tempInfo = {
                    sqno: '',
                    chdrnum: '',
                    surname: '',
                    lifcnum: '',
                    cltsex: '',
                    anbAtCcd: '',
                    statcode: '',
                    mortcls: '',
                    crtable: '',
                    sumins: '',
                    rscompcode: '',
                    riraree: '',
                    bmntdesc: '',
                    effdate: '',
                    validflag: '1',
                    cncldesc: '',
                    crtuser: '',
                    strdate: '',
                    chaguser: '',
                    enddate: '0',
                    modifyFlag: 'N',
                    saflag: '9',
                    sredno: '',
                    rownum: '',
                }
            },
            addInfo() {
                
                let that = this;

                if(that.selectRow.sqno == null || that.selectRow.sqno == "") {
                   if(this.tableData && this.tableData.length>0){
                     that.selectRow = this.tableData[0];
                   }
                }

                //初始化数据
                this.resetTempInfo();
                let para = {
                    chdrnum: that.modifyChdrnum
                }


                let flag = this.findSqnoAndSrednoByChdrnum(para);
                console.log(flag)
                console.log(that.tempInfo.sqno);


                if(this.selectRow.sqno == that.tempInfo.sqno) {
                    that.tempInfo = this.selectRow;
                    if(taht.tempInfo.validflag=='2'){
                      that.tempInfo['isEdit'] = true;
                    }else{
                      that.tempInfo['isEdit'] = false;
                    }

                    this.tableData.push(that.tempInfo);
                } else {
                    let para = {
                        chdrnum: this.modifyChdrnum
                    }
                    this.findApplnumlListByChdrnum(para)
                }


                
            },
            findCrtableListByChdrnum() {
                let that = this;
                let para = {
                    chdrnum: this.$route.query.chdrnum
                }
                if(that.crtableDetailList.length < 1) {
                    api.findCrtableListByChdrnum(para).then(res => {
                        if(res.resultCode == constant.SUCCESS && res.data && res.data.length){
                            that.crtableDetailList = res.data;
                        }
                    }).catch(err => {
                        console.log(err)
                    })
                }
            },
            findRscompcodeList() {
                let that = this;
                if(this.rscompcodeList) {
                    api.findRscompcodeList({}).then(res => {
                        if(res.resultCode == constant.SUCCESS && res.data && res.data.length){
                            that.rscompcodeList = res.data;
                        }
                    }).catch(err => {
                        console.log(err)
                    })
                }
            },
            findSqnoAndSrednoByChdrnum(para) {
                let that = this;
                api.findSqnoAndSrednoByChdrnum(para).then(res => {
                    if(res.resultCode == constant.SUCCESS && res.data){
                        that.tempInfo.sqno = res.data.sqno;
                        that.tempInfo.sredno = res.data.sredno;
                    }
                    return true;
                }).catch(err => {
                    console.log(err)
                    return false;
                })
            },

            /////////////////////////////////////////////////////////////////////////////
            findApplnumlListByChdrnum(para) {
                api.findApplnumlListByChdrnum(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        if(res.data.length != 1) {
                            // this.disabledAdd = false;
                            this.message('error', '访问数据库EUABSTM失败');
                        } else {
                            this.findMaxAgntcoyByApplnum(res.data);
                        }
                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })

                

            },
            findMaxAgntcoyByApplnum(data) {
                let is_applnum = data[0].applnum;
                this.inseagnVo.is_applnum = is_applnum;
                let para = {
                    applnum: is_applnum
                };
                if(is_applnum != null && is_applnum != "") {
                    api.findMaxAgntcoyByApplnum(para).then(res => {
                        this.$store.dispatch('loading/CHANGE_LOADING',false)
                        if(res.resultCode == constant.SUCCESS){
                            if(res.data.length != 1) {
                                this.disabledAdd = false;
                                this.message('error', '访问数据库EUAAPKY失败');
                                return ;
                            } else {
                                this.inseagnVo.ls_coy = res.data[0].agntcoy;
                            }
                        }
                    }).catch(err =>{
                        this.$store.dispatch('loading/CHANGE_LOADING',false);
                    })
                }


                api.findProcflgListByApplnum(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        data = res.data;
                        if(res.data.length != 1) {
                            this.disabledAdd = false;
                            this.message('error', '访问数据库EUAAPKY失败');
                            return;
                        } else {
                            this.inseagnVo.s_statcode = res.data[0].procflg;
                            this.inseagnVo.statcode1 = res.data[0].procflg;
                            this.findClntnumListByApplnum(para);
                        }
                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            findClntnumListByApplnum(para) {
                api.findClntnumListByApplnum(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        if(res.data.length != 1) {
                            this.disabledAdd = false;
                            this.message('error', '访问数据库euadpky失败');
                            return;
                        } else {
                            this.inseagnVo.s_cltnum = res.data[0].clntnum;
                            this.findSurnameCltsexListByClntnum(this.inseagnVo.s_cltnum);
                        }

                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            findSurnameCltsexListByClntnum(s_cltnum) {
                let para = {
                    clntnum: s_cltnum
                }
                api.findSurnameCltsexListByClntnum(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        if(res.data.length != 1) {
                            this.disabledAdd = false;
                            this.message('error', '访问数据库clnt失败');
                            return;
                        } else {
                            this.inseagnVo.s_surname = res.data[0].surname;
                            this.inseagnVo.s_cltsex = res.data[0].cltsex;
                            this.findMortclsAnbatccdListByApplnum();
                        }
                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            findMortclsAnbatccdListByApplnum() {
                let para = {
                    applnum: this.inseagnVo.is_applnum,
                    clntnum: this.inseagnVo.s_cltnum
                }
                api.findMortclsAnbatccdListByApplnum(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        if(res.data.length != 1) {
                            this.disabledAdd = false;
                            this.message('error', '访问数据库euadpky失败');
                            return;
                        } else {
                            this.inseagnVo.s_mortcls = res.data[0].mortcls;
                            this.inseagnVo.s_anb_at_ccd = res.data[0].anbAtCcd;
                            this.findStatcodeListByChdrnumChdrcoy();
                        }

                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            findStatcodeListByChdrnumChdrcoy() {
                if(this.inseagnVo.s_statcode > 4) {
                    let para = {
                        chdrnum: this.modifyChdrnum,
                        chdrcoy: this.inseagnVo.ls_coy
                    }
                    api.findStatcodeListByChdrnumChdrcoy(para).then(res => {
                        this.$store.dispatch('loading/CHANGE_LOADING',false)
                        if(res.resultCode == constant.SUCCESS){
                            if(res.data.length != 1) {
                                this.disabledAdd = false;
                                this.message('error', '访问保单主档失败');
                                return;
                            } else {
                                this.inseagnVo.s_statcode = res.data[0].statcode;
                                this.findSAnbAtCcdListByChdrnumChdrcoy(para);
                            }
                        }
                    }).catch(err =>{
                        this.$store.dispatch('loading/CHANGE_LOADING',false);
                    })
                } else {
                    this.appendInfo();
                }
            },
            findSAnbAtCcdListByChdrnumChdrcoy(para) {
                api.findSAnbAtCcdListByChdrnumChdrcoy(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        if(res.data.length != 1) {
                            this.disabledAdd = false;
                            this.message('error', '访问被保人年龄失败');
                            return;
                        } else {
                            this.inseagnVo.s_anb_at_ccd = res.data[0].anbAtCcd;
                            this.appendInfo();
                        }
                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            appendInfo() {
                this.tempInfo.chdrnum = this.modifyChdrnum;
                this.tempInfo.surname = this.inseagnVo.s_surname;
                this.tempInfo.lifcnum = this.inseagnVo.s_cltnum;
                this.tempInfo.cltsex = this.inseagnVo.s_cltsex;
                this.tempInfo.anbAtCcd = this.inseagnVo.s_anb_at_ccd;
                this.tempInfo.statcode = this.inseagnVo.s_statcode;
                this.tempInfo.mortcls = this.inseagnVo.s_mortcls;
                this.tempInfo.crtable = this.selectRow.crtable;
                this.tempInfo.sumins = this.selectRow.sumins;
                this.tempInfo.rscompcode = this.selectRow.rscompcode;
                this.tempInfo.riraree = this.selectRow.riraree;
                this.tempInfo.bmntdesc = this.selectRow.bmntdesc;
                this.tempInfo.effdate = this.nTime;
                this.tempInfo.validflag = '1'; //TODO 待定
                this.tempInfo.crtuser = this.gnvSysIsUserId;
                this.tempInfo.strdate = this.nTime;
                this.tempInfo.chaguser = '';
                this.tempInfo.enddate = '0';
                this.tempInfo.modifyFlag = 'N';
                this.tempInfo.saflag = '9';
                this.tempInfo['isEdit'] = false;
                this.tableData.push(this.tempInfo);
            },
            /////////////////////////////////////////////////////////////////////////////

            deleteInfo(index, row) {
                console.log("删除数据", row);
                if(row.rownum != '') {
                    this.message('error', '已经保存的记录不能删除!');
                    return;
                }
                if(row.saflag == '1') {
                    this.message('error', '此记录已经完成分保不能删除!');
                    return;
                }
                if(row.saflag == '9') {
                    this.deleteInfos.push(row);
                    this.tableData.splice(index,1);
                    console.info(row);
                    return;
                }
                this.$confirm('资料可能已被使用，若删除可能造成资料错误，请问是否确定删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteInfos.push(row);
                    this.tableData.splice(index,1);
                    console.info(row);
                }).catch(() => {      
                });

            },
            
            //保存信息
            saveInfo() {
                if(this.tableData.length == 0) {
                    this.message("error", "请新增数据");
                    return;
                }
                let para = {
                    chdrnum: this.modifyChdrnum
                }
                api.countInseagnByChdrnum(para).then(res => {
                    if(res.resultCode == constant.SUCCESS){
                        if(res.data > 0) {
                            this.$confirm('该保单已有分保记录您确定继续分保吗?', '提示', {
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                type: 'warning'
                            }).then(() => {
                                this.saveInfo1();
                            }).catch(() => {      
                            });
                        } else {
                            this.saveInfo1();
                        }
                    }
                }).catch(err => {
                    console.log(err);
                })
            },
            saveInfo1() {
                let checkSuccess = true;
                for(var index = 0,len = this.tableData.length; index < len; index++) {
                    let e = this.tableData[index];
                    let validflag = e.validflag;
                    let crtable = e.crtable;
                    let bmntdesc = e.bmntdesc.trim();
                    let cncldesc = e.cncldesc.trim();
                    let n_percent = 0.0;
                    if(validflag == '2') {
                        e.saflag = '2';
                        if(!cncldesc) {
                            checkSuccess = false;
                            this.message("error", '作废意见,第'+(index+1)+'行请填写作废意见')
                            return;
                        }
                    } else if(validflag = '1') {
                        e.saflag = '1';
                        if(!bmntdesc) {
                            checkSuccess = false;
                            this.message("error", '再保意见,第' + (index+1) + '行请填写再保意见')
                            return;
                        }
                        this.tableData.forEach(e => {
                            if(e.crtable == crtable && e.validflag == '1') {
                                n_percent += e.riraree;
                            }
                        })
						//小数相加会丢失精度，因而限制小数点位数
                        if(n_percent != 1 && n_percent.toFixed(5) != 1) {
                            checkSuccess = false;
                            this.message("error", '分保提示:'+crtable+'险种的分保不正确')
                            return;
                        }
                    }
                }
                // this.tableData.forEach((e, index) => {
                //     let validflag = e.validflag;
                //     let crtable = e.crtable;
                //     let bmntdesc = e.bmntdesc.trim();
                //     let cncldesc = e.cncldesc.trim();
                //     let n_percent = 0;
                //     if(validflag == '2') {
                //         e.saflag = '2';
                //         if(!cncldesc) {
                //             checkSuccess = false;
                //             this.message("error", '作废意见,第'+(index+1)+'行请填写作废意见')
                //             return;
                //         }
                //     } else if(validflag = '1') {
                //         e.saflag = '1';
                //         if(!bmntdesc) {
                //             checkSuccess = false;
                //             this.message("error", '再保意见,第' + (index+1) + '行请填写再保意见')
                //             return;
                //         }
                //         this.tableData.forEach(e => {
                //             if(e.crtable == crtable && e.validflag == '1') {
                //                 n_percent += e.riraree;
                //             }
                //         })
                //         if(n_percent != 1) {
                //             checkSuccess = false;
                //             this.message("error", '分保提示:'+crtable+'险种的分保不正确')
                //             return;
                //         }
                //     }
                // })
                if(!checkSuccess) {
                    return;
                }

                let para = {
                    saveInfo: this.tableData,
                    deleteInfo: this.deleteInfos,
                }

                api.saveData(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        this.message("success", "保存成功！")
                        this.goBack();
                    } else {
                        this.message("error", "保存失败！")
                    }
                }).catch(err =>{
                    this.message("error", "保存失败！")
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            
            validflagChange(row) {
                if(row.validflag == 2) {
                    row.effdate = '0';
                    row.enddate = this.nTime;
                    row.chaguser = this.gnvSysIsUserId;
                    row.isEdit = true;
                } else {
                    row.enddate = '0';
                    row.effdate = this.nTime;
                    row.chaguser = '';
                    row.cncldesc = '';
                    row.isEdit = false;
                }
                row.modifyFlag = 'U';
            },
            checkRiraree(row) {
                console.log(row.riraree > 1)
                if(row.riraree > 1) {
                    this.message('error', '您输入的分保比例大于1不符合分保规则');
                    return;
                }
                row.modifyFlag = 'U';
            },
            checkCrtable(row) {
                let para = {
                    chdrnum: this.modifyChdrnum,
                    crtable: row.crtable
                }
                api.checkCrtable(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS && res.data){
                        if(res.data.noticeMessage != "") {
                            this.message('error', noticeMessage);
                        }
                        if(res.data.sumins) {
                            row.sumins = res.data.sumins;
                        }
                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            message(type, msg) {
                this.$message({
                    message: msg,
                    type: type
                });
            }
        },
        computed:{
            ...mapGetters(['tags']),
            reload(){
                return this.$store.state.loading.reload;
            }
        },
        activated(){
            console.log(this.reload)
            this.initData();
            if(this.reload){
                this.$store.dispatch('loading/CHNAGE_RELOAD',false)
                Object.assign(this.$data, this.$options.data());
            }
        }
    }
</script>

<style lang="less" scoped>
    .fillcontain{
        padding-bottom: 0;
    }
    .contain{
        background: #fff;
        padding: 10px;
        margin-bottom: 20px;
    }
   .pagination{
       padding: 10px 20px;
       text-align: right;
   }
   .operationIcon{
        // padding: 12px;
        border:1px solid #e5e5e5;
        border-radius: 50%;
        width: 22px; 
        height: 22px;
        display: inline-block;
        cursor: pointer;
        box-shadow:0 0 3px #f9f9f9;
        .svg-icon{
            font-size: 14px;
            fill:#68a3fc;
        }
   }
   .search_container{
       padding: 20px 20px 0 20px;
       border-top: 1px solid #e1e1e1;
   }
</style>



